

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="Blue~u~u~u">
  <meta name="author" content="Blue~u~u~u">
  <meta name="keywords" content="">
  <meta name="description" content="迭代器模式传统模式案例：一个学校有多个学院，每个学院下面又有各种专业，但是每个学院可能存储的数据不一样，可能是集合，可能是数组，可能是map等等等，如何做到用一个统一的接口遍历全部对象， 而不暴露内部细节，简化用户的使用。 基本介绍: 迭代器模式(Iterator Pattern) 是常用的设计模式 ，属于行为型模式 如果我们的集合元素是用不同的方式实现的，有数组，还有java的集合类，或者还有其">
<meta property="og:type" content="article">
<meta property="og:title" content="java设计模式-迭代器模式">
<meta property="og:url" content="http://www.slx.blue/2021/12/08/java-iterator/index.html">
<meta property="og:site_name" content="Blue~u~u~u~u">
<meta property="og:description" content="迭代器模式传统模式案例：一个学校有多个学院，每个学院下面又有各种专业，但是每个学院可能存储的数据不一样，可能是集合，可能是数组，可能是map等等等，如何做到用一个统一的接口遍历全部对象， 而不暴露内部细节，简化用户的使用。 基本介绍: 迭代器模式(Iterator Pattern) 是常用的设计模式 ，属于行为型模式 如果我们的集合元素是用不同的方式实现的，有数组，还有java的集合类，或者还有其">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://www.slx.blue/2021/12/08/java-iterator/iterator01.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/08/java-iterator/iterator02.png">
<meta property="article:published_time" content="2021-12-08T00:27:13.000Z">
<meta property="article:modified_time" content="2021-12-08T01:56:26.919Z">
<meta property="article:author" content="Blue~u~u~u">
<meta property="article:tag" content="设计模式">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://www.slx.blue/2021/12/08/java-iterator/iterator01.png">
  
  <title>java设计模式-迭代器模式 - Blue~u~u~u~u</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.slx.blue","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":180,"cursorChar":"_","loop":true},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>

<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"/>
<script src="/live2d-widget/autoload.js"></script>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Blue~u~u</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/images/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="java设计模式-迭代器模式">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-12-08 08:27" pubdate>
        2021年12月8日 早上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      5.5k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      17 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">java设计模式-迭代器模式</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：6 个月前
                
              </p>
            
            <div class="markdown-body">
              <h3 id="迭代器模式"><a href="#迭代器模式" class="headerlink" title="迭代器模式"></a>迭代器模式</h3><p>传统模式案例：一个学校有多个学院，每个学院下面又有各种专业，但是每个学院可能存储的数据不一样，可能是集合，可能是数组，可能是map等等等，如何做到用一个统一的接口遍历全部对象， 而不暴露内部细节，简化用户的使用。</p>
<h4 id="基本介绍"><a href="#基本介绍" class="headerlink" title="基本介绍:"></a>基本介绍:</h4><ol>
<li>迭代器模式(Iterator Pattern) 是常用的设计模式 ，属于行为型模式</li>
<li>如果我们的集合元素是用不同的方式实现的，有数组，还有<code>java</code>的集合类，或者还有其他方式，当客户端要遍历这些集合元素的时候就要使用多种便利方式，而且还会暴露元素的内部结构，可以考虑使用迭代器模式解决。</li>
<li>迭代器模式，提供一种遍历集合元素的统一接口，用一致的方法遍历集合元素，不需要知道集合对象的底层表示。即：不暴露其内部结构。</li>
</ol>
<p>用例图：</p>
<p><img src="http://www.slx.blue/2021/12/08/java-iterator/iterator01.png" srcset="/img/loading.gif" lazyload alt="iterator01"></p>
<p>类图说明：</p>
<ul>
<li>Iterator ： 迭代器接口，是系统提供，含义 <code>hasNext</code>， next  ， remove)</li>
<li><code>ConcreateIterator</code>：具体的迭代器类，管理迭代。</li>
<li>Aggregate ：一个统一的聚合接口，将 客户端 和 具体聚合 解耦。</li>
<li><code>ConcreateAggreage</code>：具体的聚合 持有对象集合，并提供方法返回一个迭代器</li>
<li>Client：通过Iterator和Aggregate 依赖子类</li>
</ul>
<blockquote>
<p>编写一个学校院系结构，在一个页面中展示出学校院系组成，一个学校有多个学院，一个学院有多个系</p>
</blockquote>
<h4 id="代码分析："><a href="#代码分析：" class="headerlink" title="代码分析："></a>代码分析：</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//College接口</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">College</span> </span>&#123;<br>	<span class="hljs-comment">//名字</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span></span>;<br>	<br>	<span class="hljs-comment">//增加系的方法</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addDepartment</span><span class="hljs-params">(String name, String desc)</span></span>;<br>	<br>	<span class="hljs-comment">//返回一个迭代器,遍历</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> Iterator  <span class="hljs-title">createIterator</span><span class="hljs-params">()</span></span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//用来存储数据的统一模型</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Department</span> </span>&#123;<br><br>	<span class="hljs-keyword">private</span> String name;<br>	<span class="hljs-keyword">private</span> String desc;<br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Department</span><span class="hljs-params">(String name, String desc)</span> </span>&#123;<br>		<span class="hljs-keyword">this</span>.name = name;<br>		<span class="hljs-keyword">this</span>.desc = desc;<br>	&#125;<br>	<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>&#123;<span class="hljs-keyword">return</span> name;&#125;<br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span><span class="hljs-params">(String name)</span> </span>&#123;<span class="hljs-keyword">this</span>.name = name;&#125;<br>	<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getDesc</span><span class="hljs-params">()</span> </span>&#123;<span class="hljs-keyword">return</span> desc;&#125;<br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setDesc</span><span class="hljs-params">(String desc)</span> </span>&#123;<span class="hljs-keyword">this</span>.desc = desc;&#125;<br>	<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//计算机学院具体类</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ComputerCollege</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">College</span> </span>&#123;<br><br>	<span class="hljs-comment">//模拟每个学院存储数据的方式集合不一样</span><br>	Department[] departments;<br>	<span class="hljs-keyword">int</span> numOfDepartment = <span class="hljs-number">0</span> ;<span class="hljs-comment">// 保存当前数组的对象个数</span><br><br><br>	<span class="hljs-comment">//初始化数据</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ComputerCollege</span><span class="hljs-params">()</span> </span>&#123;<br>		departments = <span class="hljs-keyword">new</span> Department[<span class="hljs-number">5</span>];<br>		addDepartment(<span class="hljs-string">&quot;Java专业&quot;</span>, <span class="hljs-string">&quot; Java专业 &quot;</span>);<br>		addDepartment(<span class="hljs-string">&quot;PHP专业&quot;</span>, <span class="hljs-string">&quot; PHP专业 &quot;</span>);<br>		addDepartment(<span class="hljs-string">&quot;大数据专业&quot;</span>, <span class="hljs-string">&quot; 大数据专业 &quot;</span>);<br>		<br>	&#125;<br>	<br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>&#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;计算机学院&quot;</span>;<br>	&#125;<br><br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addDepartment</span><span class="hljs-params">(String name, String desc)</span> </span>&#123;<br>		Department department = <span class="hljs-keyword">new</span> Department(name, desc);<br>		departments[numOfDepartment] = department;<br>		numOfDepartment += <span class="hljs-number">1</span>;<br>	&#125;<br><br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> Iterator <span class="hljs-title">createIterator</span><span class="hljs-params">()</span> </span>&#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> ComputerCollegeIterator(departments);<br>	&#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//计算机学院迭代器</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ComputerCollegeIterator</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Iterator</span> </span>&#123;<br>	<span class="hljs-comment">//不同的迭代器实现不同的遍历方法</span><br><br>	<span class="hljs-comment">//这里我们需要Department 是以怎样的方式存放=&gt;数组</span><br>	Department[] departments;<br>	<span class="hljs-keyword">int</span> position = <span class="hljs-number">0</span>; <span class="hljs-comment">//遍历的位置</span><br>	<br><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ComputerCollegeIterator</span><span class="hljs-params">(Department[] departments)</span> </span>&#123;<br>		<span class="hljs-keyword">this</span>.departments = departments;<br>	&#125;<br><br>	<span class="hljs-comment">//判断是否还有下一个元素</span><br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">hasNext</span><span class="hljs-params">()</span> </span>&#123;<br>		<span class="hljs-keyword">if</span>(position &gt;= departments.length || departments[position] == <span class="hljs-keyword">null</span>) &#123;<br>			<span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>		&#125;<span class="hljs-keyword">else</span> &#123;<br>		<br>			<span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>		&#125;<br>	&#125;<br><br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">next</span><span class="hljs-params">()</span> </span>&#123;<br>		Department department = departments[position];<br>		position += <span class="hljs-number">1</span>;<br>		<span class="hljs-keyword">return</span> department;<br>	&#125;<br>	<br>	<span class="hljs-comment">//删除的方法，默认空实现</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">remove</span><span class="hljs-params">()</span> </span>&#123;<br>		<br>	&#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//信息工程学院具体实现类</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InfoCollege</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">College</span> </span>&#123;<br><br>	<span class="hljs-comment">//模拟每个学院存储数据的方式集合不一样</span><br>	List&lt;Department&gt; departmentList;<br>	<br>	<span class="hljs-comment">//初始化数据</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">InfoCollege</span><span class="hljs-params">()</span> </span>&#123;<br>		departmentList = <span class="hljs-keyword">new</span> ArrayList&lt;Department&gt;();<br>		addDepartment(<span class="hljs-string">&quot;信息安全专业&quot;</span>, <span class="hljs-string">&quot; 信息安全专业 &quot;</span>);<br>		addDepartment(<span class="hljs-string">&quot;网络安全专业&quot;</span>, <span class="hljs-string">&quot; 网络安全专业 &quot;</span>);<br>		addDepartment(<span class="hljs-string">&quot;服务器安全专业&quot;</span>, <span class="hljs-string">&quot; 服务器安全专业 &quot;</span>);<br>	&#125;<br>	<br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>&#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;信息工程学院&quot;</span>;<br>	&#125;<br><br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addDepartment</span><span class="hljs-params">(String name, String desc)</span> </span>&#123;<br>		Department department = <span class="hljs-keyword">new</span> Department(name, desc);<br>		departmentList.add(department);<br>	&#125;<br><br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> Iterator <span class="hljs-title">createIterator</span><span class="hljs-params">()</span> </span>&#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> InfoCollegeIterator(departmentList);<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//信息工程学院迭代器</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InfoCollegeIterator</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Iterator</span> </span>&#123;<br>	<span class="hljs-comment">//不同的迭代器实现不同的遍历方法</span><br>	<br>	List&lt;Department&gt; departmentList; <span class="hljs-comment">// 信息工程学院是以List方式存放系</span><br>	<span class="hljs-keyword">int</span> index = -<span class="hljs-number">1</span>;<span class="hljs-comment">//索引</span><br>	<br><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">InfoCollegeIterator</span><span class="hljs-params">(List&lt;Department&gt; departmentList)</span> </span>&#123;<br>		<span class="hljs-keyword">this</span>.departmentList = departmentList;<br>	&#125;<br><br>	<span class="hljs-comment">//判断list中还有没有下一个元素</span><br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">hasNext</span><span class="hljs-params">()</span> </span>&#123;<br>		<span class="hljs-keyword">if</span>(index &gt;= departmentList.size() - <span class="hljs-number">1</span>) &#123;<br>			<span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>		&#125; <span class="hljs-keyword">else</span> &#123;<br>			index += <span class="hljs-number">1</span>;<br>			<span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>		&#125;<br>	&#125;<br><br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">next</span><span class="hljs-params">()</span> </span>&#123;<br>		<span class="hljs-keyword">return</span> departmentList.get(index);<br>	&#125;<br>	<br>	<span class="hljs-comment">//空实现remove</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">remove</span><span class="hljs-params">()</span> </span>&#123;<br>		<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//统一实现打印遍历迭代</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">OutPutImpl</span> </span>&#123;<br>	<br>	<span class="hljs-comment">//学院集合</span><br> 	<span class="hljs-keyword">private</span> List&lt;College&gt; collegeList;<br><br>	<span class="hljs-comment">//有参构造实现聚合</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">OutPutImpl</span><span class="hljs-params">(List&lt;College&gt; collegeList)</span> </span>&#123;<br>		<span class="hljs-keyword">this</span>.collegeList = collegeList;<br>	&#125;<br><br>	<span class="hljs-comment">//遍历所有学院,然后调用printDepartment 输出各个学院的系</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printCollege</span><span class="hljs-params">()</span> </span>&#123;<br>		<span class="hljs-comment">//从collegeList 取出所有学院, Java 中的 List 已经实现Iterator</span><br>		Iterator&lt;College&gt; iterator = collegeList.iterator();<br>		<br>		<span class="hljs-keyword">while</span>(iterator.hasNext()) &#123;<br>			<span class="hljs-comment">//取出一个学院</span><br>			College college = iterator.next();<br>			System.out.println(<span class="hljs-string">&quot;=== &quot;</span>+college.getName() +<span class="hljs-string">&quot;===== &quot;</span> );<br>			<span class="hljs-comment">//取出学院的迭代器执行迭代方法</span><br>			printDepartment(college.createIterator()); <span class="hljs-comment">//得到对应迭代器</span><br>		&#125;<br>	&#125;<br><br>	<span class="hljs-comment">//输出 学院输出 系</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printDepartment</span><span class="hljs-params">(Iterator iterator)</span> </span>&#123;<br>		<span class="hljs-keyword">while</span>(iterator.hasNext()) &#123;<br>			Department d = (Department)iterator.next();<br>			System.out.println(d.getName());<br>		&#125;<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//客户端方法public class Client &#123;</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>		<span class="hljs-comment">//创建学校(学院集合)</span><br>		List&lt;College&gt; collegeList = <span class="hljs-keyword">new</span> ArrayList&lt;College&gt;();<br><br>		<span class="hljs-comment">//创建计算机学院和大数据学院</span><br>		ComputerCollege computerCollege = <span class="hljs-keyword">new</span> ComputerCollege();<br>		InfoCollege infoCollege = <span class="hljs-keyword">new</span> InfoCollege();<br><br>		<span class="hljs-comment">//添加计算机学院和大数据学院</span><br>		collegeList.add(computerCollege);<br>		collegeList.add(infoCollege);<br>		<span class="hljs-comment">//统一放到打印实现类实现迭代器遍历</span><br>		OutPutImpl outPutImpl = <span class="hljs-keyword">new</span> OutPutImpl(collegeList);<br>		outPutImpl.printCollege();<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="源码分析："><a href="#源码分析：" class="headerlink" title="源码分析："></a>源码分析：</h4><blockquote>
<p><code>JDK</code>中的<code>ArrayList</code>集合中就使用到了迭代器模式</p>
<p>List就是聚合接口 <code>ArrayList</code>就是聚合接口实现类 并且实现了一个特有的迭代器 Iteraror</p>
</blockquote>
<p><img src="http://www.slx.blue/2021/12/08/java-iterator/iterator02.png" srcset="/img/loading.gif" lazyload alt="List"></p>
<h4 id="注意事项和细节"><a href="#注意事项和细节" class="headerlink" title="注意事项和细节"></a>注意事项和细节</h4><ol>
<li>优点<ul>
<li>提供了一个统一的方法遍历对象，客户不再考虑聚合的类型，使用一种方法就可以遍历对象了。</li>
<li>隐藏了聚合的内部结构，客户端要遍历聚合的时候只能取到迭代器，而不会知道聚合的具体组成</li>
<li>提供了一种设计思想，就是一个类应该只有一个引起变化的原因(单一职责原则)。在聚合类中，我们吧迭代器分开，就是要把管理对象集合和遍历对象集合的责任分开，这样一来集合改变的话，只影响到集合对象。而如果便利方式改变的话，只影响到了迭代器。</li>
<li>当腰展示一组相似对象，或者遍历一组相同对象同时使用，适合使用迭代器模式</li>
</ul>
</li>
<li>缺点<ul>
<li>每个聚合对象都要一个迭代器，会生成多个迭代器不好管理类</li>
</ul>
</li>
</ol>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/designMode/">designMode</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">设计模式</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客目前大部分文章都是参考尚硅谷或者马士兵教育的学习资料！<a href="http://www.atguigu.com/" rel="nofollow noopener"官网地址！</a> 
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/12/08/spring-day04-Transactiona/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">spring-day04-transaction</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/12/07/spring-day03-AOP/">
                        <span class="hidden-mobile">spring-day03-AOP</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                

              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">

  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
	<!--《添加网站运行时间 -->
<br/>

<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
var now = new Date(); 

function createtime() {
    //此处修改你的建站时间或者网站上线时间
    var grt = new Date('11/02/2021 21:39:00');
    now.setTime(now.getTime() + 250);
    days = (now - grt) / 1000 / 60 / 60 / 24;

    dnum = Math.floor(days);
    hours = (now - grt) / 1000 / 60 / 60 - (24 * dnum);
    hnum = Math.floor(hours);
    if (String(hnum).length == 1) {
        hnum = "0" + hnum;
    }
    minutes = (now - grt) / 1000 / 60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes);
    if (String(mnum).length == 1) {
        mnum = "0" + mnum;
    }
    seconds = (now - grt) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds);
    if (String(snum).length == 1) {
        snum = "0" + snum;
    }
    document.getElementById("timeDate").innerHTML = " 本站已各种夹缝中安全运行 " + dnum + " 天 ";
    document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
}
setInterval("createtime()", 250);
</script>

<!-- 添加网站运行时间》-->
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
